package com.ninth.element.generated.number.generate.mysql.service.impl;

import com.ninth.element.generated.number.generate.common.constant.SimpleDateFormatter;
import com.ninth.element.generated.number.generate.common.pojo.enums.ResetTypeEnum;
import com.ninth.element.generated.number.generate.mysql.dao.SerialNumDao;
import com.ninth.element.generated.number.generate.mysql.pojo.entity.SerialNumEntity;
import com.ninth.element.generated.number.generate.mysql.service.SerialNumService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @author LiYUJi
 **/
public class SerialNumServiceImpl implements SerialNumService {


    private final SerialNumDao serialNumDao;

    public SerialNumServiceImpl(SerialNumDao serialNumDao) {
        this.serialNumDao = serialNumDao;
    }


    /**
     * \
     * 查询 序号
     *
     * @param serialNumType 序号类型字符串-如：拼接的字符串
     * @param isResetDaily  是否根据日期重置序号
     * @param resetType     重置序号的类型
     * @param increment     id增长步长，默认1
     * @param initial       id初始化值，默认从1开始
     * @return 序号
     */
    @Override
    public Long executeSerialNum(String serialNumType, boolean isResetDaily, ResetTypeEnum resetType, Integer increment, Long initial) {
        Long seq;
        SerialNumEntity serialNumQueryEntity = new SerialNumEntity();
        serialNumQueryEntity.setSerialNumType(serialNumType);
        this.setCycle(isResetDaily, resetType, serialNumQueryEntity);
        SerialNumEntity serialNumEntity = serialNumDao.getSerialNumEntity(serialNumQueryEntity);
        if (Objects.isNull(serialNumEntity) || Objects.isNull(serialNumEntity.getSeq())) {
            SerialNumEntity serialNumCreateEntity = new SerialNumEntity();
            serialNumCreateEntity.setCycle(serialNumQueryEntity.getCycle());
            serialNumCreateEntity.setSeq(initial);
            seq = initial;
            serialNumCreateEntity.setCreateDate(LocalDateTime.now());
            serialNumCreateEntity.setSerialNumType(serialNumType);
            serialNumDao.saveSerialNumEntity(serialNumCreateEntity);
        } else {
            seq = serialNumEntity.getSeq() + increment;
            serialNumEntity.setSeq(seq);
            serialNumDao.updateSerialNumEntity(serialNumEntity);
        }
        return seq;
    }

    private void setCycle(boolean isResetDaily, ResetTypeEnum resetType, SerialNumEntity serialNumEntity) {
        // 根据类型选择时间格式化
        if (isResetDaily) {
            DateTimeFormatter dateTimeFormatterLocal = SimpleDateFormatter.FORMATTER_DAY;
            switch (resetType) {
                case YEAR:
                    dateTimeFormatterLocal = SimpleDateFormatter.FORMATTER_YEAR;
                    break;
                case MONTH:
                    dateTimeFormatterLocal = SimpleDateFormatter.FORMATTER_MONTH;
                    break;
                case SECOND:
                    dateTimeFormatterLocal = SimpleDateFormatter.FORMATTER_SECOND;
                    break;
                case MILLISECOND:
                    dateTimeFormatterLocal = SimpleDateFormatter.FORMATTER_MILLISECOND;
                    break;
                case DAY:
                default:
                    break;
            }
            String cycle = dateTimeFormatterLocal.format(LocalDateTime.now());
            serialNumEntity.setCycle(cycle);
        }
    }

}
